﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using TMDT_Nhom1A.BUS;
using TMDT_Nhom1A.DTO;
using System.Collections.Generic;

namespace TMDT_Nhom1A.Customer
{
    public partial class ThanhToan_buoc4_NganHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //kiem tra co qua buoc 2 chưa
                Session["Step"] = 3;

                if (Session["Step"].ToString() == "3")
                {
                    //load webservice ngan hang tu database
                    List<BANK_WEBSERVICE> bankList = CouponWebservice_BUS.getAllBank(); 
                    foreach (BANK_WEBSERVICE bankItem in bankList)
                    {
                        DrlNganHang.Items.Add(new ListItem(bankItem.BankName,bankItem.Bank_WS_ID.ToString()));
                    }


                    Success.Visible = true;
                    failure.Visible = false;
                    ThongBao.Visible = false;
                    btThanhToan.Visible = true;
                    //Session["Step"] = 5;
                }
                else
                {
                    ThongBao.Visible = true;
                    failure.Visible = false;
                    Success.Visible = false;
                }
            }
        }

        protected void btThanhToan_Click(object sender, EventArgs e)
        {
            try
            {
                //sau khi nhan nut thanh toan
               

                //goi webservice cua ngan hang
                string bankID = DrlNganHang.SelectedItem.Value;
                string bankWS_Url = CouponWebservice_BUS.getBankUrl(bankID);

                BankWS.KhachHangWS bankWebservice = new TMDT_Nhom1A.BankWS.KhachHangWS();
                bankWebservice.Url = bankWS_Url;

                //lay password trong bang accountBankWebservice
                string bankUserName = "";
                string bankPassWord = "";
                CouponWebservice_BUS.getBankAccountWebservice(ref bankUserName,ref bankPassWord, bankID);
                if (bankUserName != "" && bankPassWord != "")
                {
                    string sid = bankWebservice.Authenticate(bankUserName, bankPassWord);
                    if (sid != "" && Session["UserID"].ToString() != "")
                    {
                        string userID = "";

                        //lay thong tin order
                        userID = Session["UserID"].ToString();
                        Cart cart = OrderB2B_BUS.GetSaveDetailOrderB2B(userID);

                        //tinh tong gia tri don hang 
                        float total = float.Parse(cart.CalulateTotalPrice().ToString());   //xai tam

                        //lay so the thanh toan cua doanh nghiep Biz trong bang AccountBankWebservice
                        string SoTaiKhoanBiz = CouponWebservice_BUS.getSoTheThanhToanDNBiz(bankID);
                        //goi ham transfer cua ngan hang
                        int result = bankWebservice.TransferMoney(sid,tbMaTaiKhoan.Text,bankID,SoTaiKhoanBiz,bankID,total,"VND");
                        
                        // = 1 chuyen tien thanh cong
                        if (result == 1 )
                        {
                            //chuyen tien thanh cong
                            //goi van chuyen
                            
                            
                            //lay thong tin khach hang
                            List<Customer_DTO> customerList = Customer_BUS.getCustomerInfoByUserID(int.Parse(userID));
                            Customer_DTO customerInfo = customerList.First();

                            ContactInfo contact = new ContactInfo();
                            contact.DiaChi = customerInfo.AddressCompany;
                            contact.Email = customerInfo.Email;
                            contact.SoDienThoai = customerInfo.Mobile;

                            //lay thong tin chon van chuyen
                            if (Session["TransportCompany"] != null)
                            {
                                TMDT_Nhom1A.TransportWS.TransportCompany transCompany = (TMDT_Nhom1A.TransportWS.TransportCompany)Session["TransportCompany"];
                                int quantity = 0;
                                string url = CouponWebservice_BUS.Transport(transCompany.MaCongTyVanChuyen, transCompany.HinhThucVanChuyen[0].MaHinhThucVanChuyen, cart.OrderB2BID, contact,ref quantity);
                                if (url.Contains('/'))
                                {
                                    //dat van chuyen thanh cong , thanh toan tien van chuyen cua khach hang
                                    float transportAmount = quantity * transCompany.HinhThucVanChuyen[0].Gia;
                                    int callBankresult = bankWebservice.TransferMoney(sid, tbMaTaiKhoan.Text, bankID, SoTaiKhoanBiz, bankID, transportAmount, transCompany.HinhThucVanChuyen[0].DonViTien);
                                    //int callBankresult = 1;
                                    if (callBankresult == 1)
                                    {
                                        //cap nhat trang trai cho webservice do
                                        bool updateResult = CouponWebservice_BUS.UpdateStatusOrderAfterCheckout(cart.OrderB2BID, bankID, int.Parse(transCompany.MaCongTyVanChuyen), transCompany.HinhThucVanChuyen[0].TenHinhThucVanChuyen, url);
                                        if (updateResult == true)
                                        {
                                            //thanh cong thi redirect ve trang buoc 5
                                            Session["Step"] = 4;
                                            Response.Redirect("~/Customer/ThanhToan_Buoc5_HoanThanh.aspx");
                                        }
                                        else
                                        {
                                            // thong bao thanh toan khong thanh cong
                                            Success.Visible = false;
                                            failure.Visible = true;
                                            ThongBao.Visible = false;
                                        }
                                    }
                                    else
                                    {
                                        // thong bao thanh toan khong thanh cong
                                        Success.Visible = false;
                                        failure.Visible = true;
                                        ThongBao.Visible = false;
                                    }
                                }
                                else
                                {
                                    //loi xay ra hoan tien cho khach hang va thong bao 
                                    int bankResult_1 = bankWebservice.TransferMoney(sid, SoTaiKhoanBiz, bankID, tbMaTaiKhoan.Text, bankID, total, "VND");

                                    // thong bao thanh toan khong thanh cong
                                    Success.Visible = false;
                                    failure.Visible = true;
                                    ThongBao.Visible = false;
                                }
                            }
                            else
                            {
                                //loi xay ra hoang tien cho khach hang va thong bao 
                                int bankResult_1 = bankWebservice.TransferMoney(sid, SoTaiKhoanBiz, "MSB", tbMaTaiKhoan.Text, "MSB", total, "VND");

                                // thong bao thanh toan khong thanh cong
                                Success.Visible = false;
                                failure.Visible = true;
                                ThongBao.Visible = false;


                            }
                            
                        }
                        else
                        {
                            // thong bao thanh toan khong thanh cong
                            Success.Visible = false;
                            failure.Visible = true;
                            ThongBao.Visible = false;
                        }
                    }
                    else
                    {
                        // thong bao thanh toan khong thanh cong
                        Success.Visible = false;
                        failure.Visible = true;
                        ThongBao.Visible = false;
                    }
                }
                else
                {
                    // thong bao thanh toan khong thanh cong
                    Success.Visible = false;
                    failure.Visible = true;
                    ThongBao.Visible = false;
                }


            }
            catch (Exception ex)
            {
                return;
            }

        }
    
    }
}
